package siac.com.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import siac.com.configuracao.ConexaoEclipselink;
import siac.com.entity.FacLote;


public class FacLoteDao {
	private EntityManagerFactory factory;
	private EntityManager manager;
	private FacLote lote;
	private List<FacLote> listaFacLote;

	public FacLoteDao() {
		factory = ConexaoEclipselink.getEntityManagerFactory();
	}

	private EntityManager getEntityManager() {
		return factory.createEntityManager();
	}

	public FacLote getFacLote() {
		if (this.lote == null) {
			this.lote = new FacLote();
		}
		return lote;
	}

	public void setFacLote(FacLote lote) {
		this.lote = lote;
	}

	public List<FacLote> getListaFacLote() {
		return listaFacLote;
	}

	public void setListaFacLote(List<FacLote> listaFacLote) {
		this.listaFacLote = listaFacLote;
	}

	public List<FacLote> findAll() {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery("FacLote.findAll");
			this.setListaFacLote(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacLote;
	}

	public FacLote findId(Object id) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager
					.createNamedQuery("FacLote.findById").setParameter(
							"idTabela", id);
			this.lote = (FacLote) query.getSingleResult();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.lote;
	}

	public List<FacLote> findCode(Object code) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacLote.findByCode").setParameter("code", code);
			this.setListaFacLote(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacLote;
	}

	public List<FacLote> findByDataLote(Object dataLote) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacLote.findByDataLote").setParameter("dataLote", dataLote);
			this.setListaFacLote(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacLote;
	}

	public List<FacLote> findStatus(Object status) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacLote.findByStatus").setParameter("status", status);
			this.setListaFacLote(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacLote;
	}

	public List<FacLote> findByMoeda(Object moeda) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacLote.findByMoeda").setParameter("moeda", moeda);
			this.setListaFacLote(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacLote;
	}

	public List<FacLote> findByLocAtendimento(Object localAtendimento) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacLote.findByLocAtendimento").setParameter("localAtendimento", localAtendimento);
			this.setListaFacLote(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacLote;
	}

	public String save(FacLote bean) {
		try {
			manager = getEntityManager();
			manager.getTransaction().begin();
			manager.persist(bean);
			manager.getTransaction().commit();
			return "sucesso";
		} catch (Exception ex) {
			// ex.printStackTrace();
			// /messageUtility.addMensage(ex.getMessage(), "N");
			return "erro";
		}
	}

	public String delete(FacLote bean) {
		try {
			this.setFacLote(bean);
			manager = getEntityManager();
			manager.getTransaction().begin();
			FacLote ob = manager.find(FacLote.class, getFacLote()
					.getId());
			manager.remove(ob);
			manager.getTransaction().commit();
			return "sucesso";
		} catch (Exception ex) {
			// ex.printStackTrace();
			// /messageUtility.addMensage(ex.getMessage(), "N");
			return "erro";
		}
	}

	public String update(FacLote bean) {
		try {
			manager = getEntityManager();
			manager.getTransaction().begin();
			manager.merge(bean);
			manager.getTransaction().commit();
			return "sucesso";
		} catch (Exception ex) {
			// ex.printStackTrace();
			// /messageUtility.addMensage(ex.getMessage(), "N");
			return "erro";
		}
	}
}
